package com.ruoyi.web.controller.report;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.ReportCategory;
import com.ruoyi.report.service.IReportCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报表分类Controller
 * 
 * @author ruoyi
 * @date 2022-03-03
 */

@RestController
@RequestMapping("/report/category")
public class ReportCategoryController extends BaseController
{
    @Autowired
    private IReportCategoryService reportCategoryService;

    /**
     * 查询报表分类列表
     */
//    @PreAuthorize("@ss.hasPermi('report:category:list')")
    @GetMapping("/list")
    public AjaxResult list(ReportCategory reportCategory)
    {
        startPage();
        //获取用户账户
        String username = SecurityUtils.getUsername();
        //管理员用户查询全部，其他账户只查询自己创建的模板分类
        if(!username.equals("admin")) {
            reportCategory.setCreateBy(username);
        }
        List<ReportCategory> list = reportCategoryService.selectReportCategoryList(reportCategory);
        return AjaxResult.success(list);
    }

    /**
     * 获取报表分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('report:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportCategoryService.selectReportCategoryById(id));
    }

    /**
     * 新增报表分类
     */
//    @PreAuthorize("@ss.hasPermi('report:category:add')")
    @Log(title = "报表分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportCategory reportCategory)
    {

        reportCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(reportCategoryService.insertReportCategory(reportCategory));
    }

    /**
     * 修改报表分类
     */
//    @PreAuthorize("@ss.hasPermi('report:category:edit')")
    @Log(title = "报表分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportCategory reportCategory)
    {
        reportCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(reportCategoryService.updateReportCategory(reportCategory));
    }

    /**
     * 删除报表分类
     */
//    @PreAuthorize("@ss.hasPermi('report:category:remove')")
    @Log(title = "报表分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportCategoryService.deleteReportCategoryByIds(ids));
    }
}
